<?php
/**
 * Created by Yury Patrin.
 * Time: 05.12.13 16:11
 */

class Ajax extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->library('session');
        $this->load->helper('url');

        $this->load->model('user_model');
        $this->load->model('group_model');

        $this->_checkLogin();
        
    }

    public function setUserActive()
    {
        $userId = $this->input->post('userId');
        $status = $this->input->post('val');

        $this->user_model->setUserId($userId)->setUserField('active', $status);

        if ($status == "Y")
        {
            $user = $this->user_model->getUserById($userId);

            $MailChimp = new MailChimp();
            $MailChimp->call('lists/unsubscribe', array(
                'id'                => '2a269d2ed0',
                'email'             => array('email' => $user->email),
                "delete_member"     => true,
                "send_goodbye"      => false,
                "send_notify"       => false,
            ));

            $MailChimp->call('lists/subscribe', array(
                'id'                => 'b486f9e04c',
                'email'             => array('email' => $user->email),
                'double_optin'      => false,
                'update_existing'   => true,
                'replace_interests' => false,
                'send_welcome'      => false,
            ));
        }
    }

    public function setUserGroup()
    {
        $userId = $this->input->post('userId');
        $status = $this->input->post('val');

        $this->user_model->setUserId($userId)->setUserField('group', $status);
    }

    public function deleteUser()
    {
        $userId = $this->input->post('userId');
        $this->user_model->setUserId($userId)->deleteUser();
    }

    public function createUser()
    {
        $email = $this->input->post('email');
        $passwd = $this->input->post('passwd');
        $notify = $this->input->post('notify');

        $result = $this->user_model->createUser($email, $passwd);

        if ($result['resCode'] == '00' && $notify == '1')
        {
            $this->load->library('email');
            $this->email->set_newline("\r\n");

            $this->config->load('email');
            $emailCfg = $this->config->item('email_from');

            $email_body = $this->load->view('new_user_email', '', true);
            $email_body = str_replace(['%email%','%password%','%koza_lnk%'],[$email, $passwd, $this->config->site_url()], $email_body);

            $this->email->from($emailCfg['email'], $emailCfg['name']);
            $this->email->to($email);
            $this->email->subject('KOZA: Регистрация в системе');
            $this->email->message($email_body);

            $this->email->send();
        }

        print json_encode($result);
    }

    public function updateGroup()
    {
        $groupId        = $this->input->post('groupId');
        $access_users   = $this->input->post('access_users');
        $access_groups  = $this->input->post('access_group');

        $this->group_model->updateGroup($groupId, ['access_users' => $access_users, 'access_groups' => $access_groups]);
    }

    protected function _checkLogin()
    {
        $loginStatus = $this->session->userdata('login');

        if ($loginStatus != "true")
        {
            print "Auth required";
            exit;
        }
    }
}